package com.spa.infrastructure.repository.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.util.StringUtils;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.repository.condition.OrderExtCondition;
import com.spa.infrastructure.repository.mapper.OrderInfoExtMapper;
import com.spa.infrastructure.repository.po.OrderExtPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 订单扩展  RepositoryService
 *
 * @author: lig
 * @date: 2023/12/9
 */
@Component
@Slf4j
public class OrderInfoExtRepositoryService extends ServiceImpl<OrderInfoExtMapper, OrderExtPO> {

    public boolean insertOrderExt(OrderExtPO bean) {
        if(BeanUtil.isNotEmpty(bean) && ObjectUtils.isEmpty(bean.getIsDel())){
            bean.setIsDel(0);
        }
        return save(bean);
    }

    public OrderExtPO getOrderInfoOne(OrderExtCondition condition) {
        return getOne(wrapper(condition));
    }

    public List<OrderExtPO> list(OrderExtCondition condition) {
        return list(wrapper(condition));
    }


    public boolean updateByCondition(OrderExtPO po) {
        LambdaUpdateWrapper<OrderExtPO> upWrapper = Wrappers.lambdaUpdate();
        upWrapper.set(ObjectUtil.isNotNull(po.getIsDel()), OrderExtPO::getIsDel, po.getIsDel());
        upWrapper.eq(OrderExtPO::getId, po.getId());
        return update(null, upWrapper);
    }


    private LambdaQueryWrapper<OrderExtPO> wrapper(OrderExtCondition condition) {
        LambdaQueryWrapper<OrderExtPO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollectionUtil.isNotEmpty(condition.getOrderIds()), OrderExtPO::getOrderId, condition.getOrderIds());
        wrapper.eq(StringUtils.isNotEmpty(condition.getOrderExtCode()), OrderExtPO::getOrderExtCode, condition.getOrderExtCode());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getIsDel()), OrderExtPO::getIsDel, condition.getIsDel());
        wrapper.eq(ObjectUtils.isEmpty(condition.getIsDel()), OrderExtPO::getIsDel, WhetherTypeEnum.$0.getCode());
        return wrapper;
    }

}
